package com.wanchuang.shengfubao.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.proto.Recharge;
import com.wanchuang.shengfubao.view.NumberRunningTextView;
import com.ruffian.library.widget.RTextView;

import java.util.ArrayList;


/**
 * Created by chenzhimao on 17-7-6.
 */

public class SnapHelperAdapter extends RecyclerView.Adapter<SnapHelperAdapter.GalleryViewHolder> {
    private LayoutInflater mInflater;
    private ArrayList<Recharge.today_earnings.Earningsinfo.List> mData;
    private int[] imgs = new int[]{R.mipmap.home_earn_one, R.mipmap.home_earn_two, R.mipmap.home_earn_three, R.mipmap.home_earn_four};
    private String[] colors = new String[]{"#7D2D1E", "#4E4188", "#A76D24", "#2D7782"};
    private String[] types = new String[]{" 今日收益(元) ", " 今日交易(元) ", " 今日激活(台) ", " 今日发展(个) "};
    private String[] typesTwo = new String[]{"昨日收益 ", "昨日交易", "昨日激活", "昨日发展"};


    public SnapHelperAdapter(Context context, ArrayList<Recharge.today_earnings.Earningsinfo.List> data) {
        mInflater = LayoutInflater.from(context);
        mData = data;

    }

    @Override
    public GalleryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == 0) {
            view = mInflater.inflate(R.layout.gallery_item_layout, parent, false);
        } else {
            view = mInflater.inflate(R.layout.gallery_item_layout_two, parent, false);
        }
        return new GalleryViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        return position == 3 ? 1 : 0;
    }


    @Override
    public void onBindViewHolder(final GalleryViewHolder holder, int position) {
        holder.rootView.setBackgroundResource(imgs[position % 4]);
        holder.tvTitle.setText(types[position % 4]);
        holder.tvTitle.setTextColor(Color.parseColor(colors[position % 4]));
        holder.tvYesterdayType.setText(typesTwo[position % 4]);
        holder.tvYesterdayNumber.setText(mData.get(position).getYesterday());
        holder.tvPercent.setText(mData.get(position).getRatio() + "%");
        // 修改滚动数字类型 0 money 1 number
        if (position % 4 == 2 || position % 4 == 3) holder.tvNumberView.setTextType(1);
        holder.tvNumberView.setContent(mData.get(position).getToday());

        if (mData.get(position).getRatio().contains("-")) {
            holder.tvUpDownImage.setBackgroundResource(R.mipmap.icon_down);
        } else {
            holder.tvUpDownImage.setBackgroundResource(R.mipmap.icon_up);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class GalleryViewHolder extends RecyclerView.ViewHolder {
        public RelativeLayout rootView;
        public RTextView tvTitle;
        public TextView tvYesterdayType;
        public TextView tvYesterdayNumber;
        public TextView tvPercent;
        public NumberRunningTextView tvNumberView;
        public ImageView tvUpDownImage;

        public GalleryViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.root_bg);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvYesterdayType = itemView.findViewById(R.id.tv_yesterday_type);
            tvYesterdayNumber = itemView.findViewById(R.id.tv_yesterday_number);
            tvPercent = itemView.findViewById(R.id.tv_percent);
            tvNumberView = itemView.findViewById(R.id.tvNumberView);
            tvUpDownImage = itemView.findViewById(R.id.tv_up_down_image);
        }
    }
}
